import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class AddPaperStock extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
	{
		doPost(request,response);
	}//doGet

	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
	{
		RequestDispatcher dispatcher = null;
		String event = request.getParameter("eventAction");
		if( event.equals("exit") )
		{
			dispatcher = request.getRequestDispatcher("/jsp/OperatorActivities.jsp");
			dispatcher.forward(request,response);
		}
		try
		{
			int cardid = Integer.parseInt(request.getParameter("cardid"));
			int count = Integer.parseInt(request.getParameter("count"));
			String inno = request.getParameter("inno");
			String date1 = request.getParameter("collectdate");
			String d = date1.substring(0,2);
			String m = date1.substring(3,5);
			String y = date1.substring(6,10); 
			String indate = y + "-" + m + "-" + d;
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/airtel","root","root");
			Statement stmt = con.createStatement();
			String qry = "select count from paperstock where cardType="+cardid+"";
			System.out.println("1"+qry);
			ResultSet rs = stmt.executeQuery(qry);
			int c = 0;
			int stock = 0;
			if(rs.isBeforeFirst())
			{
				while(rs.next())
				{
				c++;
				stock = rs.getInt(1);
				}
					stock += count;
			
					int id = 0;
					rs = stmt.executeQuery("select max(id) from paperstock");
					if(rs.next())
					{
						if( rs.getString(1) != null )
						{
							id = Integer.parseInt(rs.getString(1));
						}
					}
					id++;
				System.out.println("2"+id);
				String qry1 = "insert into paperstock values('"+id+"','"+stock+"','"+cardid+"','"+inno+"','"+indate+"','"+count+"')";
				System.out.println("3"+qry1);
				stmt.executeUpdate(qry1);	
					request.setAttribute("msg", new String("Paper Rc. Cards Stock entry entered successfyully"));    
					dispatcher = request.getRequestDispatcher("/jsp/PaperStock.jsp");
					dispatcher.forward(request,response);
			}
			else
			{
				int id = 0;
					rs = stmt.executeQuery("select max(id) from paperstock");
					if(rs.next())
					{
						if( rs.getString(1) != null )
						{
							id = Integer.parseInt(rs.getString(1));
						}
					}
					id++;
					stmt.executeUpdate("insert into paperstock values('"+id+"','"+count+"','"+cardid+"','"+inno+"','"+indate+"','"+count+"')");	
					request.setAttribute("msg", new String("Paper Rc. Cards Stock entry entered successfyully"));    
					dispatcher = request.getRequestDispatcher("/jsp/PaperStock.jsp");
					dispatcher.forward(request,response);

			}

		}//try
		catch(Exception e)
		{
            e.printStackTrace();
		}//catch
	}//doPost
}//class